import {
    User,
    Home
} from '@/services';
import {
    Auth
} from '@/providers';
import {
    store
} from '@/store';
import i18n from '@/i18n';
import AxiosConfig from '@/ajax/axios.config';
import {
    TOGGLESIBAR,
    SIDEBARINDEX,
    LOGINSTATUS,
    SUBARRDATA,
    USERSTATUS,
    GETUSERTATUS,
    LANGUAGETYPE,
    GETALLCONTACT,
    ORDERDETAIL,
    FIRSTLOAD
} from './mutation-types';
export default {
    // 侧边栏一级
    [TOGGLESIBAR](state) {
        state.sidebar = !state.sidebar;
    },
    // 侧边栏二级
    [SIDEBARINDEX](state, data) {
        state[data.type] = data.index;
    },
    // 获取登陆状态
    [LOGINSTATUS](state, bool) {
        state.loginStatus = bool;
    },
    [SUBARRDATA](state, data) {
        state.subNav = data;
    },
    [USERSTATUS](state, data) {
        state.userStatus = data;
    },
    // 请求接口，获取账户的验证状态
    async [GETUSERTATUS](state, next) {
        try {
            let statusData = await User.GetUserState({
                userNo: Auth.getItem('userId'),
                token: Auth.getItem('token')
            });
            if (statusData.code == '301') {
                if (next) next();
                return;
            }
            store.dispatch('USERSTATUS', statusData.data);
        } catch (error) {
            console.log(error);
        }
    },
    // 判断是否第一次登录
    [FIRSTLOAD](state, bool) {
        state.fistLoad = bool;
    },
    // 请求头的LanguageType ,做到中英切换无刷新
    [LANGUAGETYPE](state) {
        Auth.getItem('lang') == 'zh' ? Auth.setItem('lang', 'en') : Auth.setItem('lang', 'zh');
        i18n.locale = Auth.getItem('lang');
        AxiosConfig.headers.LanguageType = Auth.getItem('lang');
    },
    // 获取logo,首页信息
    async [GETALLCONTACT](state) {
        try {
            let res = await Home.GetAllContact({
                LanguageType: Auth.getItem('lang')
            });
            state.allcontact = res.data[0];
        } catch (error) {
            console.log(error);
        }
    }
};